<?php

namespace app\erp\controller;

class Material extends Base
{
    public function __construct($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name)
    {
        parent::__construct($client_id, $client_secret,$account_id,$user_name);

        $this->starttime = $starttime;
        $this->endtime   = $endtime;
    }
    public function run()
    {
        foreach ($this->org as $item)
        {
            $this->processSurfaceGroup($item['fnumber'], $item['id']);
        }
    }

    private function processSurfaceGroup($createorg_number, $forig) {
        $pageNo = 1;
        $pageSize = $this->pageSize;

        for ($i = $pageNo; $i <= $this->pageNo; $i++)
        {

            $response = $this->getSurfaceGroupPage($createorg_number, $i, $pageSize);
            // 检查响应是否有效
            if ($response !== null) {

                $this->insertDataAndUpdateSuper($response['data'], $forig);

            }else{
                break;
            }
        }
    }

    private function getSurfaceGroupPage($createorg_number, $pageNo, $pageSize)
    {
        $method  = "v2/bdl/bdl_comm_basedata/bdl_bd_surface/querySurface";

        $fullurl = $this->url . $method;

        $response_data = [
            'data'      => [
                'starttime'             => $this->starttime,
                'endtime'               => $this->endtime,
                'createorg_number'      => $createorg_number,
            ],
            'pageNo'    => $pageNo,
            'pageSize'  => $pageSize,
        ];

        $this->curl->setHeader("Content-Type", "application/json");
        $this->curl->setHeader("accesstoken", $this->getToken());
        $this->curl->setHeader("Idempotency-Key", uuid());

        $res = $this->curl->post($fullurl, json_encode($response_data));




        if ($res && is_object($res) && $res->status == 1)
        {
            if($res->data->totalCount > 0){

                return [
                    'data' => $res->data->rows,
                ];
            }

        }else{
            if(strlen($res->message) > 255)
            {
//                $res->message = substr($res->message,0,155);

                return $this->error( 'aaaaaaaa'  . request()->action() .  $res->message);

            }
            if(property_exists($res,'errorCode') && $res->errorCode == 404)
            {
                return $this->error( 'aaaaaaaa'  . request()->action() .  $res->message);
            }

            return [
                'data' => false,
            ];
        }


    }

    private function insertDataAndUpdateSuper($data, $forgid)
    {
        if(!empty($data))
        {
            // 组装 data 并插入
            foreach ($data as $k => $v)
            {

                //判断是否已经存在
                $isExist = \app\admin\model\Surface::where('fnumber', $v->number)->where('forgid',$forgid)->where('fk_bdl_bd_datacenter',$this->account_id)->find();

                $insertData = [
                    'fname'                             => $v->name,
                    'fnumber'                           => $v->number,
                    'fenable'                           => $v->enable,
                    'forgid'                            => $forgid,
                    'fk_bdl_comment'                    => $v->group_number,
                    'fk_bdl_createtime'                 => $v->createtime,
                    'fk_bdl_modifytime'                 => $v->modifytime,
                    'fk_bdl_typenormal'                 => $v->bdl_typenormal,
                    'fk_bdl_typecolourless'             => $v->bdl_typecolourless,
                    'fk_bdl_issystem'                   => $v->bdl_issystem,
                    'fk_bdl_shortname'                   => $v->bdl_shortname,
                    'fk_bdl_bd_datacenter'                 => $this->account_id

                ];

                if(!empty($isExist))
                {
                    unset($insertData['forgid']);
                    unset($insertData['fk_bdl_bd_datacenter']);
                    \app\admin\model\Surface::where('id',$isExist['id'])->update($insertData);
                }else{
                    \app\admin\model\Surface::create($insertData);

                }
            }

            // 插入成功后 更新 fk_bdl_super
            $this->updateSurfaceCategory($forgid);
        }

    }

    public function updateSurfaceCategory($forgid)
    {
        $data = \app\admin\model\Surface::where('forgid',$forgid)->where('fk_bdl_bd_datacenter',$this->account_id)->select();

        foreach ($data as $k=>$v)
        {
            $super = \app\admin\model\Surfacecate::where('fnumber',$v['fk_bdl_comment'])->where('forgid',$forgid)->where('fk_bdl_bd_datacenter',$this->account_id)->find();

            if($super)
            {
                \app\admin\model\Surface::where('id', $v['id'])->update(['fk_bdl_bdgroup' => $super['id']]);
            }
        }
    }


}